package com.example.javamediaserver.controller;

import com.example.javamediaserver.model.Recording;
import com.example.javamediaserver.service.RecordingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/recordings")
@RequiredArgsConstructor
@Slf4j
@CrossOrigin
public class RecordingController {

    private final RecordingService recordingService;
    
    // 获取所有录制文件
    @GetMapping
    public ResponseEntity<Map<String, Object>> getAllRecordings() {
        try {
            List<Recording> recordings = recordingService.getAllRecordings();
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("count", recordings.size());
            response.put("recordings", recordings);
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取录制文件列表失败", e);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("error", e.getMessage());
            
            return ResponseEntity.status(500).body(response);
        }
    }
    
    // 按应用和流密钥获取录制文件
    @GetMapping("/{app}/{streamKey}")
    public ResponseEntity<Map<String, Object>> getRecordingsByAppAndStreamKey(
            @PathVariable String app, 
            @PathVariable String streamKey) {
        try {
            List<Recording> recordings = recordingService.getRecordingsByAppAndStreamKey(app, streamKey);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("count", recordings.size());
            response.put("recordings", recordings);
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取录制文件列表失败: {}/{}", app, streamKey, e);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("error", e.getMessage());
            
            return ResponseEntity.status(500).body(response);
        }
    }
    
    // 删除录制文件
    @DeleteMapping("/{recordingId}")
    public ResponseEntity<Map<String, Object>> deleteRecording(@PathVariable String recordingId) {
        try {
            boolean deleted = recordingService.deleteRecording(recordingId);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", deleted);
            
            if (deleted) {
                response.put("message", "Recording deleted successfully");
                return ResponseEntity.ok(response);
            } else {
                response.put("error", "Recording not found or could not be deleted");
                return ResponseEntity.status(404).body(response);
            }
        } catch (Exception e) {
            log.error("删除录制文件失败: {}", recordingId, e);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("error", e.getMessage());
            
            return ResponseEntity.status(500).body(response);
        }
    }
    
    // 刷新录制文件列表（从磁盘扫描）
    @PostMapping("/refresh")
    public ResponseEntity<Map<String, Object>> refreshRecordings() {
        try {
            recordingService.updateRecordingsFromDisk();
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "Recordings refreshed from disk");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("刷新录制文件列表失败", e);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("error", e.getMessage());
            
            return ResponseEntity.status(500).body(response);
        }
    }
} 